package org.jeecg.modules.charge.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.charge.*;
import org.jeecg.modules.charge.service.*;
import org.jeecg.modules.charge.vo.ChargeCollect;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
* @Description: 收费对外接口
* @Author: Lee
* @Date:   2019-08-22
* @Version: V1.0
*/
@Slf4j
@Api(tags="收费feign接口")
@RestController
@RequestMapping("/api/charge")
public class ChargeDataApiController {
   @Autowired
   private IChargeCollectService chargeCollectService;
   @Autowired
   private ISysUserRoleService sysUserRoleService;
   @Autowired
   private IChargeBillsService chargeBillsService;
   @Autowired
   private IChargeStandardService chargeStandardService;
   @Autowired
   private IChargeFeeitemService chargeFeeitemService;
   @Autowired
   private IChargeFeeRuleRoomService feeRuleRoomService;


    @AutoLog(value = "API收费标准-分页列表查询")
    @ApiOperation(value = "API收费标准-分页列表查询", notes = "API收费标准-分页列表查询")
    @GetMapping(value = "/chargeStandardList")
    public Result<Page<ChargeStandard>> chargeStandardList(@RequestParam(name = "roomId") String roomId,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
                                                   ) {
        Result<Page<ChargeStandard>> result = new Result<Page<ChargeStandard>>();
        try {
            Page<ChargeStandard> page = new Page<ChargeStandard>(pageNo, pageSize);
            ChargeStandard chargeStandard = new ChargeStandard();
            chargeStandard.setRoomId(roomId);
            List<ChargeStandard> list = chargeStandardService.findPage(page,chargeStandard);
            page.setRecords(list);
            result.setSuccess(true);
            result.setResult(page);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    @AutoLog(value = "批量保存应收账单")
    @ApiOperation(value="批量保存应收账单", notes="批量保存应收账单")
    @PostMapping(value = "/bills/saveChargeBillsBatch")
    public Boolean saveChargeBillsBatch(String chargeBillsList) {
        List<ChargeBills> list = JSONObject.parseArray(chargeBillsList, ChargeBills.class);
        if (list == null || list.size() == 0) {
            return false;
        }
        return chargeBillsService.saveBatch(list);
    }

    @AutoLog(value = "单个保存应收账单")
    @ApiOperation(value="单个保存应收账单", notes="单个保存应收账单")
    @PostMapping(value = "/bills/saveChargeBills")
    public Boolean saveChargeBills(@RequestBody ChargeBills chargeBills) {
        try {
            return  chargeBillsService.save(chargeBills);
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 根据对应的人员获取对应部门领用和共享部门领用的所有票据
     */
    @AutoLog(value = "获取对应收缴率")
    @ApiOperation(value="获取对应收缴率", notes="获取对应收缴率")
    @PostMapping(value = "/getCollect")
    public Result<ChargeCollect> getCollect(String cny,String projectId) {
        ChargeCollect collect=new ChargeCollect();
        try{
            if(null==cny || StringUtils.isEmpty(cny)){
                cny=String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
            }
            Double shouldMoney=chargeCollectService.getYearShouldMoney(cny,projectId);
            Double realMoney=chargeCollectService.getYearRealMoney(cny, projectId);
            collect.setYearShouldMoney(shouldMoney);
            collect.setYearRealMoney(realMoney);
            collect.setCny(cny);
            boolean isEqual=equal(shouldMoney,0);
            if(isEqual){
                collect.setYearCollect(0.00);
            }else{
               Double c=(realMoney/shouldMoney)*100;
               BigDecimal big = new BigDecimal(c);
                c = big.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                collect.setYearCollect(c);
            }
            return Result.ok(collect);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("");
        }
    }

    @ApiOperation(value="getByFeeName", notes="getByFeeName")
    @PostMapping(value = "/getByFeeName")
    public List<ChargeFeeitem> getByFeeName (@RequestParam("feeName") String feeName){
        return chargeFeeitemService.queryItemByName(feeName);
    }

    public boolean equal(double a, double b) {
        if ((a- b> -0.000001) && (a- b) < 0.000001)
            return true;
        else
            return false;
    }
    /**
     *生成房间下所有账单
     */
    @RequestMapping("/createChargeBills")
    public Result<String> createChargeBills(@RequestBody ChargeGenerateBills chargeGenerateBills){
        List<String> ruleIds = Arrays.asList(chargeGenerateBills.getFeeRuleId().split(","));
        try {
            if (ruleIds.size() > 0){
                for (String ruleId:ruleIds){
                    chargeGenerateBills.setFeeRuleId(ruleId);
                    Result<String> chargeBillsNew = chargeBillsService.createChargeBillsNew(chargeGenerateBills);
                }
            }
            return Result.ok("生成账单成功");
        }catch (Exception e){
            return Result.error("生成账单失败");
        }
    }

    //根据合同生成所有账单
    @PostMapping("/createChargeBillsContractNew")
    public Result<String> createChargeBillsContractNew(@RequestBody ChargeGenerateBills chargeGenerateBills){
        Result<String> chargeBillsNew = chargeBillsService.createChargeBillsContractNew(chargeGenerateBills);
        return chargeBillsNew;
    }

    /**
     * 房间未缴账单
     */
    @RequestMapping("queryRoomBills")
    public  List<ChargeBills>  queryRoomBills(@RequestParam("roomIdList")List roomIdList,@RequestParam("spiltDate")String spiltDate){
        List<ChargeBills> chargeBills = chargeBillsService.queryRoomBills(roomIdList,spiltDate);
        return chargeBills;
    }
    /**
     * 获取合同下所有账单
     */
    @RequestMapping("queryContractAllBills")
    public  List<ChargeBills>  queryContractAllBills(@RequestParam("contractId")String contractId, @RequestParam("zjCny")String zjCny){
        List<ChargeBills> chargeBills = chargeBillsService.queryContractAllBills(contractId,zjCny);
        return chargeBills;
    }

    /**
     * 作废账单
     */
    @RequestMapping("batchBillDelete")
    public void batchBillDelete(@RequestParam("ids")List<String> ids){
        chargeBillsService.batchBillDelete(ids);
    }

    /**
     * 房间所有账单
     */
    @RequestMapping("queryRoomAllBills")
    public  List<ChargeBills>  queryRoomAllBills(@RequestParam("roomIdList")List<String> roomIdList){
        List<ChargeBills> chargeBills = chargeBillsService.queryRoomAllBills(roomIdList);
        return chargeBills;
    }

    /**
     * 合同未缴账单
     */
    @RequestMapping("/queryContractBills")
    public  List<ChargeBills>  queryContractBills(@RequestParam("contractId")String contractId){
        List<ChargeBills> chargeBills = chargeBillsService.queryContractBills(contractId);
        return chargeBills;
    }
    /**
     * 生成 账单
     */
    @RequestMapping("/canCellAndGenerateRoomBills")
    public    Result<?> canCellAndGenerateRoomBills( @RequestBody  List<CanGeBillsVo> genaBillsVoList
                                                    ){ 
         Result<Object> result = new Result<>();
         try { 
            if (genaBillsVoList.size() > 0){
                for (CanGeBillsVo cgb:genaBillsVoList){
                     //获取房间收费规则
                     String feeId =  feeRuleRoomService.selectFeeId(cgb.getRoomId(),cgb.getFeeItemId());
                     ChargeGenerateBills chargeGenerateBills = new ChargeGenerateBills();
                     chargeGenerateBills.setStartDate(cgb.getStartDate());
                     chargeGenerateBills.setEndDate(cgb.getPayDate());
                     chargeGenerateBills.setGenerateMode(1);
                     chargeGenerateBills.setRoomIds(Arrays.asList(cgb.getRoomId().split(",")));
                     chargeGenerateBills.setFeeRuleId(feeId);
                     chargeGenerateBills.setTenantId(cgb.getTenantId());
                     Result<String> chargeBillsNew = chargeBillsService.createChargeBillsNew(chargeGenerateBills);
                 }
             }
            result.setMessage("操作成功");
             result.setSuccess(true);
          }catch (Exception e){
             e.getMessage();
             return Result.error("操作失败");
         } 

     return result;
    }
    /**
     * 单房间最大已缴费账单
     */
    @RequestMapping("getMaxPayBills")
    public ChargeBills getMaxPayBills(@RequestParam("roomId")String roomId){

        ChargeBills maxPayBills = chargeBillsService.getMaxPayBills(roomId);

        return maxPayBills;
    }
    /**
     * 作废大于截止日期账单
     * @Param roomId spiltDate
     */
   @RequestMapping("canCellBills")
   public  boolean canCellBills (@RequestParam("roomIds")List<String> roomIds,@RequestParam("spiltDate")String spiltDate){
       try {
           chargeBillsService.canCellBills(roomIds,spiltDate);
       }catch (Exception e){
           log.info("错误信息",e.getMessage());
           return false;
       }
       return true;

   }/**
     * 作废大于截止日期账单
     * @Param roomId spiltDate
     */
   @RequestMapping("canCellAllBills")
   public  boolean canCellAllBills (@RequestParam("roomIds")List<String> roomIds){
       try {
           chargeBillsService.canCellAllBills(roomIds);
       }catch (Exception e){
           log.info("错误信息",e.getMessage());
           return false;
       }
       return true;
   }
    /**
     * 批量插入
     */
    @RequestMapping("saveBatchFeeRoom")
    public void saveBatchFeeRoom(@RequestBody  List<ChargeFeeRuleRoom> chargeFeeRuleRoomList){
        feeRuleRoomService.saveBatch(chargeFeeRuleRoomList);
    }
}
